package com.bsoft.gol.editor.fileUpload;


import com.bsoft.gol.editor.editor.EditorUser;
import com.bsoft.gol.editor.model.UserInExcel;
import opensource.jpinyin.PinyinHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;


@Service("userDataTranslator")
public class UserDataTranslator extends AbstractTranslator<UserInExcel, UserHolder> {
    private static final Logger logger = LoggerFactory.getLogger(UserDataTranslator.class);
    @Override
    public void preTranslate(TableData<UserInExcel> tableData) {
        if (tableData.getCount().get() > 0) {
        }
    }

    @Override
    public TableData<UserHolder> postTranslate(TableData<UserInExcel> tableData) {
        TableData<UserHolder> result = new TableData<UserHolder>();
        if (tableData.getCount().get() > 0) {
            List<UserInExcel> orgRows = tableData.getRowData();
            for (UserInExcel dataInfoExcel : orgRows) {
                UserHolder holder = new UserHolder();
                holder.setRowIndex(dataInfoExcel.getRowIndex());
                //实例化
                EditorUser editorUser=new EditorUser();
                BeanUtils.copyProperties(dataInfoExcel, editorUser);
                editorUser.setLoginUserId(UUID.randomUUID().toString());
                editorUser.setCreateTime(new Date());
                holder.setUserInExcel(dataInfoExcel);
                holder.setEditorUser(editorUser);

                result.addRowDataItem(holder);
            }
        }
        return result;
    }

    @Override
    public void afterTranslate(TableData<UserHolder> tableData) {
        //数据校验
        UserValidator validator = new UserValidator();
        validator.validate(tableData);

    }
}
